package com.ruoyi.web.controller.system.app;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.UtilTool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.AppVersion;
import com.ruoyi.system.service.IAppVersionService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * app版本Controller
 * 
 * @author ruoyi
 * @date 2021-07-15
 */
@Controller
@RequestMapping("/system/version")
public class AppVersionController extends BaseController
{
    private String prefix = "system/version";

    @Autowired
    private IAppVersionService appVersionService;

    /**
     * 查询app版本列表
     */
    //@PreAuthorize("@ss.hasPermi('system:version:list')")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(AppVersion appVersion)
    {
        startPage();
        List<AppVersion> list = appVersionService.selectAppVersionList(appVersion);
        return getDataTable(list);
    }

    /**
     * 导出app版本列表
     */
    //@PreAuthorize("@ss.hasPermi('system:version:export')")
    @Log(title = "app版本", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(AppVersion appVersion)
    {
        List<AppVersion> list = appVersionService.selectAppVersionList(appVersion);
        ExcelUtil<AppVersion> util = new ExcelUtil<AppVersion>(AppVersion.class);
        return util.exportExcel(list, "app版本数据");
    }

    /**
     * 新增保存app版本
     */
    //@PreAuthorize("@ss.hasPermi('system:version:add')")
    @Log(title = "app版本", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(AppVersion appVersion)
    {
        // 获取当前的用户名称
        String username = SecurityUtils.getUsername();
        appVersion.setAppSysUser(username);
        appVersion.setType(0);
        appVersion.setCreateTime(new Date(System.currentTimeMillis()));
        appVersion.setUpdateTime(new Date(System.currentTimeMillis()));
        AppVersion version = appVersionService.getNewestAppVersion();
        if (!UtilTool.isNull(version)){
            version.setType(1);
            appVersionService.updateAppVersion(version);
        }
        return toAjax(appVersionService.insertAppVersion(appVersion));
    }

    /**
     * 修改保存app版本
     */
    //@PreAuthorize("@ss.hasPermi('system:version:edit')")
    @Log(title = "app版本", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(AppVersion appVersion)
    {
        appVersion.setUpdateTime(new Date(System.currentTimeMillis()));
        return toAjax(appVersionService.updateAppVersion(appVersion));
    }

    /**
     * 删除app版本
     */
    //@PreAuthorize("@ss.hasPermi('system:version:remove')")
    @Log(title = "app版本", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(appVersionService.deleteAppVersionByIds(ids));
    }
}
